package com.hbue.springboot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbue.springboot.mapper.TbAccountMapper;
import com.hbue.springboot.mapper.TbCustomerMapper;
import com.hbue.springboot.mapper.TbHistoryMapper;
import com.hbue.springboot.mapper.TbOrderMapper;
import com.hbue.springboot.pojo.DTO.TbHistoryDTO;
import com.hbue.springboot.pojo.TbCustomer;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.pojo.TbOrder;
import com.hbue.springboot.service.TbCustomerService;
import com.hbue.springboot.vo.AddOrderVo;
import com.hbue.springboot.vo.QueryParamsVo;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TbCustomerServiceImpl implements TbCustomerService {
    private final TbCustomerMapper customerMapper;

    private final TbHistoryMapper tbHistoryMapper;

    private final TbOrderMapper tbOrderMapper;

    private final TbAccountMapper tbAccountMapper;

    private static final Logger log = LoggerFactory.getLogger(TbAccountServiceImpl.class);

    @Override
    public int updateCustomer(TbCustomer customerDTO) {
//        TbCustomer customer = new TbCustomer();
//        DeepBeanUtils.copyProperties(customerDTO, customer);
        return customerMapper.updateById(customerDTO);
    }
    @Override
    public TbCustomer getCustomerByName(String name){
        return  customerMapper.getCustomerByName(name);
    }

    @Override
    public IPage<TbHistory> getOrderWithoutFinish(QueryParamsVo params) {
        log.info("list order without finish ="+params);
        Page<TbHistory> page = new Page<>(params.getPageNum(), params.getPageSize());
        String name = (String) params.getParams().get("name");
        Integer id = customerMapper.selectByName(name).getId();
        IPage<TbHistory> list = tbHistoryMapper.getOrderWithoutFinish(page,id);
        if(list == null) { // 订单不存在
            return null;
        }
        return list;
    }

    /**
     * 取消订单
     * 1.检查订单是否存在
     * 2.检查订单是否已经完成
     *  2.1 订单完成,无法取消
     * 3.订单存在且未完成,返回顾客该订单花费的水票数量
     * 4.更新顾客水票数
     * 5.添加一条新的历史记录
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelOrder(Integer id) {
        TbHistory history = tbHistoryMapper.selectById(id);
        if(history == null) {
            return -1;
        }

        if(history.getSendWaterTime() != null) {
            return -3;
        }

        TbCustomer customer = customerMapper.selectById(history.getCustId());
        if(customer == null) {
            return -2;
        }

        customer.setCustTicket(customer.getCustTicket() + history.getSendWaterCount());
        customerMapper.updateById(customer);

        history.setSendWaterCount(history.getSendWaterCount());
        history.setVersion(404L);
        Integer row = null;
        if (history.getWorkerId() != null) {
            tbHistoryMapper.updateById(history);
            row = tbHistoryMapper.insert(new TbHistory(history.getCustId(), history.getSendWaterCount() * -1, history.getWorkerId(), 404L));
        } {
            tbHistoryMapper.updateById(history);
            row = tbHistoryMapper.insert(new TbHistory(history.getCustId(), history.getSendWaterCount() * -1, 404L));
        }

        if(row <= 0) {
            throw new RuntimeException("添加失败");
        }
        return row;
    }

    @Override
    public String getCustomerName(Integer id) {
        return customerMapper.getCustomerName(id);
    }

    @Override
    public String getCustomerAddress(Integer id) {
        return customerMapper.getCustomerAddress(getCustomerName(id));
    }

    /**
     * 添加订单
     * 1.根据传入的客户名 获取客户信息
     * 2.根据客户信息 获取客户余额
     * 3.如果余额不足,返回-3
     * 4.如果余额足够,更新客户余额,添加一条新的历史记录
     * @param order
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrder(AddOrderVo order) {
        // 获取客户名
        String name = order.getCustomerName();
        // 获取客户信息
        TbCustomer customer = customerMapper.selectByName(name);
        if(customer == null) {
            return -2;
        }
        // 查询余额是否足够
        Integer count = customer.getCustTicket();
        if(count < order.getCount()) { // 余额不足
            return -3;
        }

        customer.setCustTicket(count - order.getCount());
        customerMapper.updateById(customer);

        Integer id = tbHistoryMapper.insert(new TbHistory(customer.getId(), order.getCount()));
        if(id == null || id <= 0) {
            throw new RuntimeException("添加失败");
        }
        return id;
    }
}